﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Entities;
using ExpertMIS.Brokers;

namespace ExpertMIS.Admin.UserControl
{
    public partial class ExpertRegisterForm : System.Web.UI.UserControl
    {
        #region 控件事件处理

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RefreshExpert();
            }
        }

        #endregion

        #region 公有方法


        public ADMUserEntity Entity
        {
            get
            {
                return ViewState["_ADMUserEntity_"] as ADMUserEntity;
            }
            set
            {
                ViewState["_ADMUserEntity_"] = value;
            }
        }

        public bool SaveExpertUser()
        {
            try
            {
                // 检查用户名是否有效
                string account = txAccount.Text;
                ADMUserBroker broker = new ADMUserBroker();
                if(broker.GetUserByAccount(account) != null)
                {
                    return false;
                }

                Entity = new ADMUserEntity();
                Entity.Account = txAccount.Text;
                Entity.Password = SYS.Common.EncryptHelper.MD5Encrypt(txPassword.Text);
                Entity.Name = txName.Text;
                Entity.Email = txEmail.Text;
                Entity.Enable = true;
                Entity.IsPassed = true;
                Entity.IsExpert = true;
                Entity.IsAdmin = false;

                string errorInfo = "";
                bool result = broker.RegisterUser(Entity,ref errorInfo );
                if (result)
                {
                    Entity.OID = errorInfo;
                }
                else
                {
                    return false;
                }

                string ExpertID = ddlExpert.SelectedValue;
                ExpertBroker expertbroker = new ExpertBroker();
                if (!expertbroker.UpdateExpertUserID(ExpertID, Entity.OID))
                {
                    broker.RemoveEntity(Entity, null);
                    return false;
                }
                
                return true;
            }
            catch
            {
                return false;
            }
        }

        public void RefreshExpert()
        {
            ExpertBroker expertBroker = new ExpertBroker();
            IList<ExpertEntity> list = expertBroker.GetUnRegisterExperts();

            if (list != null)
            {
                ExpertEntity entity = new ExpertEntity();
                entity.OID = "";
                entity.Name = "<空>";
                list.Insert(0, entity);
            }

            ddlExpert.DataSource = list;
            ddlExpert.DataTextField = "Name";
            ddlExpert.DataValueField = "OID";
            ddlExpert.DataBind();
        }

        protected void ddlExpert_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlExpert.SelectedItem.Text == "<空>")
            {
                this.txName.Text = "";
                this.txEmail.Text = "";
                return;
            }

            ExpertBroker broker = new ExpertBroker();
            ExpertEntity entity = broker.GetExpertByOID(ddlExpert.SelectedValue);
            if (entity != null)
            {
                this.txName.Text = entity.Name;
                this.txEmail.Text = entity.EMail;
            }
            else
            {
                this.txName.Text = "";
                this.txEmail.Text = "";
            }
        }

        #endregion
    }
}